package com.miguo.activity.fragment;

import android.os.Bundle;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.miguo.activity.LoginActivity;
import com.miguo.activity.base.BaseFragment;
import com.miguo.app.AppHelper;
import com.miguo.view.listener.SingleClickEvent;
import com.miguo.framework.request.JsonData;
import com.miguo.framework.utils.Toast;
import com.miguo.framework.view.CustomAutoCompleteTextView;
import com.miguo.http.HttpClient;
import com.miguo.http.Url;
import com.miguo.http.callback.AjaxCallBack;
import com.miguo.ui.R;
import com.miguo.utils.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * File Names: RegisterFragment
 * Describe:
 * User: LinXiaoZhi(87100854@qq.com)
 * Date: 2015-08-12
 * Time: 17:57
 */
public class RegisterFragment extends BaseFragment implements View.OnClickListener {

    private View contentView;

    private String mUserName, mUserEmail, mPassword, mConfirmPassword;

    private boolean isHidePwd = false;

    private LoginActivity mLoginActivity;

    private ImageView mHidePwdIv;

    private CustomAutoCompleteTextView mNameEdt, mEmailEdt, mPwdEdt, mConfirmPwdEdt;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_register, container, false);
        return contentView;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mLoginActivity = (LoginActivity) this.getActivity();
        initTitle();
        initView();
        initListener();
    }

    private void initTitle() {
        mLoginActivity.mBaseHead.getRightTextView().setVisibility(View.VISIBLE);
        mLoginActivity.mBaseHead.setTitle(R.string.login_register);
        mLoginActivity.mBaseHead.getRightTextView().setText("登录");
        mLoginActivity.mBaseHead.getRightTextView().setOnClickListener(new SingleClickEvent() {
            @Override
            public void singleClick(View view) {
                mLoginActivity.replaceFragment(LoginFragment.class);
            }
        });
    }

    private void initView() {
        mNameEdt = (CustomAutoCompleteTextView) contentView.findViewById(R.id.register_name_edt);
        mEmailEdt = (CustomAutoCompleteTextView) contentView.findViewById(R.id.register_email_edt);
        mPwdEdt = (CustomAutoCompleteTextView) contentView.findViewById(R.id.register_pwd_edt);
        mConfirmPwdEdt = (CustomAutoCompleteTextView) contentView.findViewById(R.id.register_confirm_pwd_edt);

        mHidePwdIv = (ImageView) contentView.findViewById(R.id.register_hide_confirm_pwd_iv);
    }

    private void initListener() {
        mHidePwdIv.setOnClickListener(this);
        contentView.findViewById(R.id.register_btn).setOnClickListener(this);
        contentView.findViewById(R.id.register_open_taobao_tv).setOnClickListener(this);
        contentView.findViewById(R.id.register_open_qq_tv).setOnClickListener(this);
    }

    private boolean verify() {
        mUserName = mNameEdt.getText().toString();
        mUserEmail = mEmailEdt.getText().toString();
        mPassword = mPwdEdt.getText().toString();
        mConfirmPassword = mConfirmPwdEdt.getText().toString();
        if (mUserName.length() == 0) {
            mNameEdt.setShakeAnimation();
            Toast.show(mContext, "请输入用户名");
            return false;
        }
        if (mUserEmail.length() == 0) {
            mEmailEdt.setShakeAnimation();
            Toast.show(mContext, "请输入邮箱");
            return false;
        }
        if (mPassword.length() == 0) {
            mPwdEdt.setShakeAnimation();
            Toast.show(mContext, "请输入密码");
            return false;
        }

        if (mUserName.length() < 2) {
            mNameEdt.setShakeAnimation();
            Toast.show(mContext, "用户名格式错误");
            return false;
        }

        if (StringUtils.isEmail(mUserEmail) == false) {
            mEmailEdt.setShakeAnimation();
            Toast.show(mContext, "邮箱格式不正确");
            return false;
        }

        if (mPassword.length() < 6 || mPassword.length() > 16) {
            mPwdEdt.setShakeAnimation();
            Toast.show(mContext, "密码格式错误");
            return false;
        }

        if (mConfirmPassword.equals(mPassword) == false) {
            mConfirmPwdEdt.setShakeAnimation();
            Toast.show(mContext, "两次密码输入不一致");
            return false;
        }
        return true;
    }

    private void register() {
        AjaxCallBack callBack = new AjaxCallBack(mActivity, "正在注册,请稍候...") {
            @Override
            public void onRequestFinish(JsonData json) {
                super.onRequestFinish(json);

                if (json.optInt("status") == 200) {
                    mLoginActivity.mUserName = mUserName;
                    mLoginActivity.mPassword = mPassword;
                    mLoginActivity.login();
                    return;
                }
                Toast.show(mContext, json.optString("info"));
            }
        };
        Map<String, String> param = new HashMap<String, String>();
        param.put("username", mUserName);
        param.put("password", mPassword);
        param.put("confirm_password", mConfirmPassword);
        param.put("email", mUserEmail);
        HttpClient.postEncryptData(Url.Passport.User.Register, param, callBack);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.register_hide_confirm_pwd_iv:
                if (isHidePwd) {
                    //隐藏密码
                    isHidePwd = false;
                    mConfirmPwdEdt.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    mConfirmPwdEdt.setSelection(mConfirmPwdEdt.getText().toString().length());
                    mHidePwdIv.setImageResource(R.drawable.ico_login_eye_h);
                } else {
                    //显示密码
                    isHidePwd = true;
                    mConfirmPwdEdt.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    mConfirmPwdEdt.setSelection(mConfirmPwdEdt.getText().toString().length());
                    mHidePwdIv.setImageResource(R.drawable.ico_login_eye);
                }
                break;
            case R.id.register_btn:
                if (verify()) {
                    register();
                }
                break;
            case R.id.register_open_qq_tv:
                AppHelper.StartAuthWeb(mActivity, Url.Passport.Oauth.QQ);
                break;
            case R.id.register_open_taobao_tv:
                AppHelper.StartAuthWeb(mActivity, Url.Passport.Oauth.TaoBao);
                break;
        }
    }
}